<template>
  <!-- 部门的每一行 -->
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width:100%"
  >
    <el-col>
      <strong>
        <!-- 根组件显示的 公司名 负责人 操作 -->
        <strong v-if="isRoot">
          <i class="el-icon-office-building" />
          {{ nodeData.name }}</strong>
        <!-- 子组件显示的 总裁办 管理员？ 操作 -->
        <span v-else>
          {{ nodeData.name }}
        </span>
      </strong></el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span class="optBtn">
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDepartment">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="editDepartment">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepartment">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/departments'
export default {
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    // 是否根组件
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 删除部门
    async delDepartment() {
      // 提示确定
      await this.$confirm('确定删除此部门？', '警告')
      // 调用接口
      await delDepartment(this.nodeData.id)
      // 提示成功
      this.$message.success('删除成功')
      // 子传父 更新数据
      this.$emit('delDepartment')
    },
    // 点击新增部门 显示模态框 事件响应给父组件
    addDepartment() {
      this.$emit('addDepartment', this.nodeData)
    },
    // 点击编辑部门 显示模态框 事件响应给父组件
    editDepartment() {
      this.$emit('editDepartment', this.nodeData)
    }
  }
}
</script>

<style>
.optBtn {
    cursor: pointer;
  }
</style>
